#include "stdafx.h"
#include "../vfs/UncompressedLibraryLocation.h"

namespace ja2{ namespace vfs
{
	//! Iterator implementation for uncompressed libraries.
	class UncompressedLibraryLocation::IteratorImpl : public ILocation::Iterator::IImplementation
	{
		// Attributes
	private:
		//! Actual location.
		DirCatalog_t::const_iterator m_Iter;

		// Operations
	private:
		//! Get value.
		virtual DirectoryPtr_t value() const;
		//! Move to next.
		virtual void increment();
		//! Compare with other.
		virtual bool equal(IImplementation const &Right) const;
		//! Clone the instance.
		virtual Ptr_t clone() const;

		// Construction
	public:
		IteratorImpl(DirCatalog_t::const_iterator const &Iterator);
	};

	UncompressedLibraryLocation::IteratorImpl::IteratorImpl(UncompressedLibraryLocation::DirCatalog_t::const_iterator const &Iterator)
		: m_Iter(Iterator)
	{
	}
	DirectoryPtr_t UncompressedLibraryLocation::IteratorImpl::value() const
	{
		return m_Iter->second;
	}

	void UncompressedLibraryLocation::IteratorImpl::increment()
	{
		++m_Iter;
	}

	bool UncompressedLibraryLocation::IteratorImpl::equal(IImplementation const &Right) const
	{
		// Right type
		if(typeid(*this) == typeid(Right))
			return (m_Iter == static_cast<IteratorImpl const &>(Right).m_Iter);

		return false;
	}

	UncompressedLibraryLocation::IteratorImpl::Ptr_t UncompressedLibraryLocation::IteratorImpl::clone() const
	{
		return Ptr_t(new IteratorImpl(m_Iter));
	}

	//-------------------------------//

	UncompressedLibraryLocation::UncompressedLibraryLocation(boost::filesystem::path const &LibraryFilePath, boost::filesystem::path const &MountPoint) 
		: m_Path(LibraryFilePath), m_MountPoint(MountPoint)
	{
	}

	bool UncompressedLibraryLocation::dir_exist(boost::filesystem::path const &Path) const
	{
		return (m_DirectoryMap.count(Path) == 1);
	}

	DirectoryPtr_t UncompressedLibraryLocation::get_dir(boost::filesystem::path const &Path) const
	{
		return m_DirectoryMap.find(Path)->second;
	}

	void UncompressedLibraryLocation::add_directory(boost::filesystem::path const &Path, DirectoryPtr_t &pDir)
	{
		assert(!dir_exist(Path));

		m_DirectoryMap[Path] = pDir;
	}

	void UncompressedLibraryLocation::set_mount_point(boost::filesystem::path const &Path)
	{
		m_MountPoint = Path;
	}

	boost::filesystem::path const &UncompressedLibraryLocation::get_mount_point() const
	{
		return m_MountPoint;
	}

	boost::filesystem::path const &UncompressedLibraryLocation::get_path() const
	{
		return m_Path;
	}

	PathVector_t UncompressedLibraryLocation::get_sub_dirs() const
	{
		PathVector_t out;
		for(auto it = m_DirectoryMap.begin(), end = m_DirectoryMap.end(); it != end; ++it)
			out.push_back(it->first);

		return std::move(out);
	}

	UncompressedLibraryLocation::Iterator UncompressedLibraryLocation::begin()
	{
		return Iterator(new UncompressedLibraryLocation::IteratorImpl(m_DirectoryMap.begin()));
	}

	UncompressedLibraryLocation::Iterator UncompressedLibraryLocation::end()
	{
		return Iterator(new UncompressedLibraryLocation::IteratorImpl(m_DirectoryMap.end()));
	}
} /*vfs*/} /*ja2*/